﻿using System;
using System.Collections.Specialized;
using System.Net;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Newtonsoft.Json.Linq;
using Terraria.GameContent.UI.Elements;
using Terraria.Localization;
using Terraria.ModLoader.IO;
using Terraria.UI;
using Terraria.UI.Gamepad;

namespace Terraria.ModLoader.UI
{
	// Token: 0x0200028F RID: 655
	internal class UIManagePublished : UIState
	{
		// Token: 0x06001A77 RID: 6775 RVA: 0x00429690 File Offset: 0x00427890
		public override void OnInitialize()
		{
			UIElement uielement = new UIElement();
			uielement.Width.Set(0f, 0.8f);
			uielement.MaxWidth.Set(600f, 0f);
			uielement.Top.Set(220f, 0f);
			uielement.Height.Set(-220f, 1f);
			uielement.HAlign = 0.5f;
			UIPanel uipanel = new UIPanel();
			uipanel.Width.Set(0f, 1f);
			uipanel.Height.Set(-110f, 1f);
			uipanel.BackgroundColor = new Color(33, 43, 79) * 0.8f;
			uielement.Append(uipanel);
			this.myPublishedMods = new UIList();
			this.myPublishedMods.Width.Set(-25f, 1f);
			this.myPublishedMods.Height.Set(0f, 1f);
			this.myPublishedMods.ListPadding = 5f;
			uipanel.Append(this.myPublishedMods);
			UIScrollbar uiscrollbar = new UIScrollbar();
			uiscrollbar.SetView(100f, 1000f);
			uiscrollbar.Height.Set(0f, 1f);
			uiscrollbar.HAlign = 1f;
			uipanel.Append(uiscrollbar);
			this.myPublishedMods.SetScrollbar(uiscrollbar);
			this.uITextPanel = new UITextPanel<string>(Language.GetTextValue("tModLoader.MBMyPublishedMods"), 0.8f, true);
			this.uITextPanel.HAlign = 0.5f;
			this.uITextPanel.Top.Set(-35f, 0f);
			this.uITextPanel.SetPadding(15f);
			this.uITextPanel.BackgroundColor = new Color(73, 94, 171);
			uielement.Append(this.uITextPanel);
			UITextPanel<string> uitextPanel = new UITextPanel<string>(Language.GetTextValue("UI.Back"), 1f, false);
			uitextPanel.VAlign = 1f;
			uitextPanel.Height.Set(25f, 0f);
			uitextPanel.Width.Set(-10f, 0.5f);
			uitextPanel.Top.Set(-20f, 0f);
			uitextPanel.OnMouseOver += UICommon.FadedMouseOver;
			uitextPanel.OnMouseOut += UICommon.FadedMouseOut;
			uitextPanel.OnClick += UIManagePublished.BackClick;
			uielement.Append(uitextPanel);
			base.Append(uielement);
		}

		// Token: 0x06001A78 RID: 6776 RVA: 0x00012E0D File Offset: 0x0001100D
		private static void BackClick(UIMouseEvent evt, UIElement listeningElement)
		{
			Main.PlaySound(11, -1, -1, 1, 1f, 0f);
			Main.menuMode = 10001;
		}

		// Token: 0x06001A79 RID: 6777 RVA: 0x00012E2E File Offset: 0x0001102E
		public override void Draw(SpriteBatch spriteBatch)
		{
			base.Draw(spriteBatch);
			UILinkPointNavigator.Shortcuts.BackButtonCommand = 100;
			UILinkPointNavigator.Shortcuts.BackButtonGoto = 10001;
		}

		// Token: 0x06001A7A RID: 6778 RVA: 0x00429918 File Offset: 0x00427B18
		public override void OnActivate()
		{
			this.myPublishedMods.Clear();
			this.uITextPanel.SetText(Language.GetTextValue("tModLoader.MBMyPublishedMods"), 0.8f, true);
			string json = "";
			try
			{
				ServicePointManager.Expect100Continue = false;
				string address = "http://javid.ddns.net/tModLoader/listmymods.php";
				NameValueCollection values = new NameValueCollection
				{
					{
						"steamid64",
						ModLoader.SteamID64
					},
					{
						"modloaderversion",
						ModLoader.versionedName
					},
					{
						"passphrase",
						ModLoader.modBrowserPassphrase
					}
				};
				byte[] bytes = UploadFile.UploadFiles(address, null, values);
				json = Encoding.UTF8.GetString(bytes);
			}
			catch (WebException ex)
			{
				if (ex.Status == WebExceptionStatus.Timeout)
				{
					this.uITextPanel.SetText(Language.GetTextValue("tModLoader.MenuModBrowser") + " " + Language.GetTextValue("tModLoader.MBOfflineWithReason", Language.GetTextValue("tModLoader.MBBusy")), 0.8f, true);
					return;
				}
				this.uITextPanel.SetText(Language.GetTextValue("tModLoader.MenuModBrowser") + " " + Language.GetTextValue("tModLoader.MBOfflineWithReason", ""), 0.8f, true);
				return;
			}
			catch (Exception e)
			{
				ErrorLogger.LogModBrowserException(e);
				return;
			}
			try
			{
				JArray jarray = JArray.Parse(json);
				foreach (JObject jobject in jarray.Children<JObject>())
				{
					UIModManageItem item = new UIModManageItem((string)jobject["displayname"], (string)jobject["name"], (string)jobject["version"], (string)jobject["author"], (string)jobject["downloads"], (string)jobject["downloadsversion"], (string)jobject["modloaderversion"]);
					this.myPublishedMods.Add(item);
				}
			}
			catch (Exception e2)
			{
				ErrorLogger.LogModBrowserException(e2);
			}
		}

		// Token: 0x040017A5 RID: 6053
		private UIList myPublishedMods;

		// Token: 0x040017A6 RID: 6054
		public UITextPanel<string> uITextPanel;
	}
}
